package com.xingdata.jjxc.m.login.avt;

import java.util.HashMap;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.LoginActivity;
import com.xingdata.jjxc.base.Base1Activity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.utils.ActivityTools;
import com.xingdata.jjxc.utils.ImageUtils;
import com.xingdata.jjxc.utils.MessageCode;
import com.xingdata.jjxc.views.AnimationView;

public class FindPwd2 extends Base1Activity implements OnClickListener {

	private EditText resetpwd_pwd1, resetpwd_pwd2;
	private Button resetpwd_btn;
	private String phone;
	private String checknum;
	private Handler mHandler;
	private ImageView imageView_pic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle bundle = getIntent().getExtras();
		phone = bundle.getString("phone");
		checknum = bundle.getString("checknum");
		// ExitApplication.getInstance().addActivity(this);
	}

	@Override
	protected int getContentViewId() {
		return R.layout.activity_find_pwd2;
	}

	@Override
	protected String getTitleText() {
		return "重置密码";
	}

	@Override
	protected void initViews() {
		resetpwd_pwd1 = (EditText) findViewById(R.id.resetpwd_pwd1_et);
		resetpwd_pwd2 = (EditText) findViewById(R.id.resetpwd_pwd2_et);
		resetpwd_btn = (Button) findViewById(R.id.resetpwd_btn);
		imageView_pic = (ImageView) findViewById(R.id.imageView_pic);
		resetpwd_btn.setOnClickListener(this);

		mHandler = new Handler() {
			public void handleMessage(android.os.Message msg) {
				switch (msg.what) {

				case MessageCode.MessageCode_ShowToast:
					showToast(msg.getData().getString("result"));
					break;

				default:
					break;
				}
			};
		};
		new AnimationView(this, imageView_pic).init();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.resetpwd_btn:
			String pswStr = resetpwd_pwd1.getText().toString().trim();
			String pswRewStr = resetpwd_pwd2.getText().toString().trim();

			if (pswStr.length() < 6) {
				showToast(R.string.error_pwd_length);
			} else if (!pswStr.equals(pswRewStr)) {
				showToast(R.string.error_pwd_not_same);
				resetpwd_pwd2.setText("");
			} else {
				doPost_forgetpwd(phone, pswStr, checknum);
			}
			break;

		default:
			break;
		}
	}

	private void doPost_forgetpwd(final String mobile, final String password,
			final String checknum) {
		if (isNetworkConnected() == false) {
			showToast("网络连接断开，请检查网络");
			return;
		}

		paramsMap = new HashMap<String, String>();
		paramsMap.put("mobile", mobile);
		paramsMap.put("password", password);
		paramsMap.put("checknum", checknum);

		mHttpUtil.Post(App.ZZD_REQUEST_FORGETPWD, paramsMap,
				new HttpResponseListener() {
					@Override
					public void onSuccess(RespEntity respEntity) {
						Intent intent = new Intent();
						intent.setClass(FindPwd2.this, LoginActivity.class);
						Bundle bundle = new Bundle();
						bundle.putString("mobile", mobile);
						intent.putExtras(bundle);
						ActivityTools.startActivity(FindPwd2.this, intent);
						finish();
					}

					@Override
					public void onFailure(String result) {
						Bundle bundle = new Bundle();
						bundle.putString("result", result);
						Message message = new Message();
						message.setData(bundle);
						message.what = MessageCode.MessageCode_ShowToast;
						mHandler.sendMessage(message);

					}

					@Override
					public void onLoading(int vurrent) {
						// TODO Auto-generated method stub

					}
				});
	}

//	@Override
//	protected void onDestroy() {
//		// TODO Auto-generated method stub
//		super.onDestroy();
//		ImageUtils.releaseImageViewResouce(imageView_pic);
//	}
}
